<template>
  <el-tabs v-model="activeName" @tab-click="changeTab">
    <el-tab-pane label="按金额" name="user">
      <div ref="chart" :class="className" :style="{height:height,width:width,padding:padding}" />
    </el-tab-pane>
    <el-tab-pane label="按笔数" name="code">
      <div ref="charts" :class="className" :style="{height:height,width:width,padding:padding}" />
    </el-tab-pane>
  </el-tabs>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    padding: {
      type: String,
      default: '16px 16px 0'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      activeName: 'user'
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    changeTab() {
      if (this.activeName === 'user') {
        this.initChart()
      } else {
        this.$nextTick(() => {
          this.refChart()
        })
      }
    },
    initChart() {
      this.chart = echarts.init(this.$refs.chart, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['长款', '短款', '平款']
        },
        series: [
          {
            name: '总交易金额',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: 320, name: '长款' },
              { value: 240, name: '短款' },
              { value: 149, name: '平款' },
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    },
    refChart() {
      this.chart = echarts.init(this.$refs.charts, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['长款', '短款', '平款']
        },
        series: [
          {
            name: '总交易金额',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: 200, name: '长款' },
              { value: 170, name: '短款' },
              { value: 149, name: '平款' },
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .el-tabs {
    /deep/ .el-tabs__header {
      margin: 0;
      background: #fff;
      padding-left: 20px;
    }
    /deep/ .el-tabs__nav-wrap::after{
      height: 1px;
    }
  }
</style>
